package com.yunwushanhe.controller;



import com.yunwushanhe.annotation.SystemLog;
import com.yunwushanhe.bean.req.UserLoginReq;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.UserInfoVO;
import com.yunwushanhe.bean.vo.UserLoginVO;
import com.yunwushanhe.entity.User;
import com.yunwushanhe.enums.AppHttpCodeEnum;
import com.yunwushanhe.exception.BlogSystemException;
import com.yunwushanhe.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户表(User)表控制层 用于实现登录请求
 *
 * @author 抬头看看天
 * @since 2023-08-05 22:32:05
 */
@RestController
@RequestMapping
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login") // 带请求体的请求，可以用 @RequestBody 注解来接收
    public ResponseResult<UserLoginVO> login(@RequestBody UserLoginReq userLoginReq){
        if(!StringUtils.hasText(userLoginReq.getUserName()) || !StringUtils.hasText(userLoginReq.getPassword())){
            throw new BlogSystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return userService.login(userLoginReq);
    }

    @PostMapping("/logout")
    public ResponseResult<Object> logout(){
        return userService.logout();
    }

    @GetMapping("/user/userInfo")
    public ResponseResult<UserInfoVO> getUserInfo(){
        return userService.getUserInfo();
    }

    @PutMapping("/user/userInfo")
    @SystemLog(businessName = "更新用户信息")
    public ResponseResult<Object> updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }

    @PutMapping("/user/register")
    public ResponseResult<Object> register(@RequestBody User user){
        return userService.register(user);
    }

}

